<template>
	<div class="zs-table-page-container">
		<div class="zs-card-sm zs-space-bottom zs-layout-flex-row zs-layout-justify-between">
			<span>
				<el-input v-model="projectName" class="zs-select-md zs-space-right-sm" placeholder=" " disabled />
				<el-cascader class="zs-select-md zs-space-right-sm" @change="cascaderChange" v-model="queryParams.inventoryClass" :options="cascaderData" :props="cascaderProps" clearable placeholder="清单分类" filterable />
				<el-input v-model="queryParams.inventoryName" class="zs-select-md zs-space-right-sm" placeholder="清单名称" clearable filterable @blur="queryList" />
				<!-- <el-select v-model="queryParams.inventoryId" class="zs-select-md zs-space-right-sm" placeholder="清单名称" clearable filterable @change="queryList">
					<el-option v-for="item in inventoryList.data" :key="item.id" :label="item.name" :value="item.id" />
				</el-select> -->
				<el-popover popper-class="zs-popper" placement="bottom-end" trigger="click">
					<template #reference>
						<el-button class="zs-btn-square-solid" :class="{ 'zs-btn-square-solid-active': queryParams.roleKey || queryParams.planName || queryParams.processNam || queryParams.effectiveState }">
							<el-icon>
								<MoreFilled />
							</el-icon>
						</el-button>
					</template>
					<template #default>
						<el-select v-model="queryParams.roleKey" class="zs-select-md zs-space-right-sm" placeholder="角色名称" clearable filterable @change="queryList">
							<el-option v-for="item in roleList" :key="item.roleKey" :label="item.roleName" :value="item.roleKey" />
						</el-select>
						<el-input v-model="queryParams.planName" class="zs-select-md zs-space-right-sm" placeholder="计划名称" clearable @blur="queryList" />
						<el-input v-model="queryParams.processName" class="zs-select-md zs-space-right-sm" placeholder="流程名称" clearable @blur="queryList" />
						<el-select v-model="queryParams.effectiveState" class="zs-select-md zs-space-right-sm zs-space-top-sm" placeholder="生效状态" @change="queryList" :teleported="false">
							<el-option v-for="item in dictObj.VALIDATE_FLAG" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />
						</el-select>
					</template>
				</el-popover>
				<el-button class="zs-btn-plain" @click="queryList" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_query'] }"> 查询 </el-button>
			</span>
			<span>
				<el-button class="zs-btn-md zs-btn-primary" @click="openDrawer(false, 'add', {}, 'base')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_add'], businessFlag: true }">新增</el-button>
			</span>
		</div>

		<!-- teble -->
		<div class="zs-card zs-table-page-content">
			<el-table class="zs-table-page-main" :data="tableData.data" v-loading="tableData.loading" stripe>
				<el-table-column type="index" label="序号" width="60" align="center" :index="index => index + 1 + (tableData.currentPage - 1) * curPageSize" />
				<el-table-column prop="inventoryName" label="清单名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="roleName" label="角色名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="planName" label="计划名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="processName" label="流程名称" minWidth="120" show-overflow-tooltip align="left" :formatter="defaultTableData" />
				<el-table-column prop="enableStatusName" label="启用状态" minWidth="145" align="left" show-overflow-tooltip :formatter="defaultTableData" />
				<el-table-column prop="effectiveState" label="生效状态" align="center">
					<template #default="{ row }">
						<span :class="row.effectiveState == 1 ? 'zs-validata-status-yes' : 'zs-validata-status-no'">{{ row.effectiveState == 1 ? '有效' : '失效' }}</span>
					</template>
				</el-table-column>
				<el-table-column prop="" label="定义" width="120" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(true, 'info', row, 'base')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_view'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'base')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_edit'], businessFlag: true }">编辑</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="设备" width="150" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="openDrawer(true, 'info', row, 'equipment')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_device_query'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'equipment')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_device_edit'], businessFlag: true }">配置</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="openDrawer(false, 'edit', row, 'standard')" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_device_standard'], businessFlag: true }" :disabled="row.deviceFlag ? false : true">标准</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="" label="计划" width="120" align="center">
					<template #default="{ row }">
						<el-button class="zs-link" link size="small" @click="setRow(row, true)" :disabled="row.planFlag ? false : true" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_plan_query'] }">查看</el-button>
						<el-divider direction="vertical" />
						<el-button class="zs-link" link size="small" @click="setRow(row, false)" :disabled="row.planFlag ? false : true" v-btnPermi="{ permis: ['p_btn_property_maintenance_list_plan_edit'], businessFlag: true }">配置</el-button>
					</template>
				</el-table-column>
				<el-table-column prop="level1Name" label="一级分类" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="level2Name" label="二级分类" show-overflow-tooltip align="center" :formatter="defaultTableData" />
				<el-table-column prop="level3Name" label="三级分类" show-overflow-tooltip align="center" :formatter="defaultTableData" />
			</el-table>
			<div class="zs-space-top-sm zs-table-page-pagination">
				<el-pagination v-model:currentPage="tableData.currentPage" v-model:page-size="curPageSize" :page-sizes="curPageSizes" :small="true" :background="true" layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="pageChange('size', $event)" @current-change="pageChange('page', $event)" />
			</div>
		</div>
	</div>

	<!-- 定义抽屉 -->
	<DrawerPlan ref="baseRef" :isShow="drawer.base.isShowDrawer" :isReadOnly="drawer.base.isReadOnly" :handleType="drawer.base.handleType" :currentRow="currentRow.data" @close="closeDrawer($event, 'base')"> </DrawerPlan>

	<!-- 设备抽屉 -->
	<DrawerPlanEquipment ref="equipmentRef" :isShow="drawer.equipment.isShowDrawer" :isReadOnly="drawer.equipment.isReadOnly" :handleType="drawer.equipment.handleType" :currentRow="currentRow.data" @close="closeDrawer($event, 'equipment')"> </DrawerPlanEquipment>

	<!-- 计划 -->
	<SetPlanDialog ref="plan" @queryList="queryList" @saveCb="planBind"></SetPlanDialog>

	<!-- 标准 -->
	<DrawerPointStandard :isShow="drawer.standard.isShowDrawer" :isReadOnly="drawer.standard.isReadOnly" :handleType="drawer.standard.handleType" :currentRow="currentRow.data" @close="closeDrawer($event, 'standard')"> </DrawerPointStandard>
</template>

<script setup>
	import DrawerPlan from './components/drawerPlan.vue';
	import DrawerPlanEquipment from './components/drawerPlanEquipment.vue';
	import DrawerPointStandard from './components/drawerPointStandard.vue';
	import SetPlanDialog from '@/components/drawerPlan.vue';
	import { getAllClassificationDataApi, getInventoryApi } from '@/api/common.js';
	import { planMaintainListApi, planBindCbApi, planRoleDropApi } from '@/api/equipment.js';
	import { ref, reactive } from 'vue';
	import { useStore } from 'vuex';
	import { useRoute } from 'vue-router';
	import { defaultTableData } from '@/utils/publicUtils';

	const route = useRoute();
	let store = useStore();
	const curPageSizes = ref(store.state.uiPageSizes);
	const curPageSize = ref(store.state.uiPageSizeDefault);
	const projectName = ref(decodeURI(route.query.projectName || ""));

	let dictObj = store.getters.getDictObj(['VALIDATE_FLAG', 'BUILDING_LEVEL']);

	// 列表 filed
	let tableData = reactive({
		currentPage: 1,
		total: 0,
		loading: false,
		data: [],
	});

	//查询条件
	let queryParams = reactive({ projectCode: route.query.projectCode, pageNumber: 1, pageSize: curPageSize.value, inventoryName: '', planName: '', processName: '', effectiveState: '1' });

	// 级联规则
	const cascaderProps = reactive({
		checkStrictly: true,
		value: 'code',
		label: 'dictLabel',
	});
	// 级联数据
	let cascaderData = ref([]);
	// 级联变更
	const cascaderChange = val => {
		let cur = ['level1', 'level2', 'level3'];
		if (val) {
			cur.forEach((el, index) => {
				queryParams[el] = val[index] || '';
			});
			if (queryParams.level3) {
				getInventoryList();
			}
		} else {
			cur.forEach(ele => {
				queryParams[ele] = '';
			});
			inventoryList.data = [];
		}
		queryList();
	};
	//清单名称数据源
	let inventoryList = reactive({ data: [] });
	const getInventoryList = async () => {
		let res = await getInventoryApi({
			classify1: queryParams.level1,
			classify2: queryParams.level2,
			classify3: queryParams.level3,
		});
		if (res.data.code !== 200) return;
		inventoryList.data = res.data.data || [];
	};

	// 获取全部分类数据
	const queryAllClassificationData = async () => {
		let res = await getAllClassificationDataApi();
		if (res.data.code !== 200) return;
		cascaderData.value = res.data.data || [];
	};

	// 抽屉相关
	let currentRow = reactive({ data: {} });
	let drawer = reactive({
		base: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		equipment: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
		standard: {
			handleType: '',
			isReadOnly: false,
			isShowDrawer: false,
		},
	});

	const openDrawer = (flag, handleType, row, drawerType) => {
		drawer[drawerType].isReadOnly = flag;
		drawer[drawerType].isShowDrawer = true;
		drawer[drawerType].handleType = handleType;
		currentRow.data = row;
	};

	const closeDrawer = (data, drawerType) => {
		drawer[drawerType].isShowDrawer = data.isShow;
		queryList('init');
	};

	// 列表数据源
	const queryList = async init => {
		if (process.env.NODE_ENV !== 'development') {
			let portalResourceList = JSON.parse(localStorage.getItem('portalResourceList'));
			if (init != 'init' && !portalResourceList.includes('p_btn_property_maintenance_list_query')) return;
		}
		tableData.loading = true;
		let res = await planMaintainListApi(queryParams);
		if (res.data.code !== 200) return;
		tableData.data = res.data.data.list || [];
		tableData.total = res.data.data.total || 0;
		tableData.loading = false;
	};

	//流程列表
	let roleList = ref([]);
	const getRoleList = async () => {
		let res = await planRoleDropApi();
		if (res.data.code !== 200) return;
		roleList.value = res.data.data || [];
	};

	//配置计划后的回调
	const planBind = async () => {
		await planBindCbApi({ id: currentRow.data.id });
		queryList();
	};

	// 分页
	const pageChange = (type, size) => {
		if (type == 'size') {
			queryParams.pageSize = size;
		} else {
			queryParams.pageNumber = size;
		}
		queryList('init');
	};

	//计划相关
	const plan = ref();
	const setRow = (row, planReadonly) => {
		currentRow.data = row;
		plan.value.setRow(row, planReadonly);
	};

	getRoleList();
	queryList('init');
	queryAllClassificationData();
</script>

<style scoped lang="scss"></style>
